package com.chuangke.admin.manager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;

@Component
public class CacheAdminManager {
	
	@Autowired
	private CacheManager cacheManager;
	
	/**
	 * 获取所有缓存区域
	 * @return
	 */
	public List<Map<String,Object>> getAllRegion(){
//		return cacheChannel.regions() ;
		Collection<String> caches = cacheManager.getCacheNames() ;
		List<Map<String,Object>> records = new ArrayList<>() ;
		if(CollectionUtils.isEmpty(caches)) {
			return records ;
		}
		
		Map<String,Object> record = null ;
		for(String cache : caches) {
			record = new HashMap<>() ;
			record.put("code", cache) ;
			record.put("name", cache) ;
//			record.put("size", ) ;
//			record.put("ttl", ) ;
			
			records.add(record) ;
		}
		
		return records ;
	}
	
	/**
	 * 获取所有缓存区域
	 * @return
	 */
	public void clearRegion(String region){
//	   cacheChannel.clear(region); ;
	}
	
	/**
	 * 删除缓存
	 * @param region
     */
	public void removeRegion(String region) {
//		cacheChannel.removeRegion(region); ;
	}
	
	/**
	 * 失效缓存
	 * @param region
	 * @param keys
	 */
	public void evict(String region, String...keys) {
//		cacheChannel.evict(region, keys);
		Arrays.asList(keys).forEach(key ->cacheManager.getCache(region).evict(key));
	}
	
	/**
	 * 查看缓存
	 * @param region
	 * @param key
	 */
	public Object getCache(String region,String key) {
//		return cacheChannel.get(region, key, false) ;
		return null ;
	}
}
